<?php
namespace App\Service\Member;

class SeekService extends BaseService
{
    //咨询

    /**
     * 添加
     * @param int $did 医生sID
     * @param int $sid 咨询sID
     * @param int $paid 病人paID
     */
    public function getOne($did, $sid, $patid)
    {
        $this->guoqi();
        //医生
        $dOne = (array) $this->dbConn($this->dbDoctor)
            ->where('del', 0)
            ->where('id', $did)
            ->first();
        if (empty($dOne)) {
            //return $this->toback(-2, '没有记录');
            return $this->toback();
        }
        if (!$dOne['seek_type'] || !$sid) {
            //return $this->toback(-2, '没有记录');
            return $this->toback();
        }
        $seekOne = (array) $this->dbConn($this->dbCate)
            ->where('del', 0)
            ->where('type', 4)
            ->first();
        if (empty($seekOne)) {
            return $this->toback(-2, '咨询类型错误');
        }
        $res = (array) $this->dbConn($this->dbUdsPatient)
            ->where('doctor_id', $did)
            ->where('seek_id', $sid)
            ->where('patient_id', $patid)
            ->whereIn('is_pay', [1, 2])
            ->orderBy('id', 'asc')
            ->first();
        if (empty($res) || $res['is_pay'] > 2) {
            $zxid = $this->dbConn($this->dbUdsPatient)->insertGetId([
                'doctor_id' => $did, 'seek_id' => $sid,
                'patient_id' => $patid, 'addtime' => toDate(),
            ]);
            $arys = [
                'zx_id' => $zxid, 'did' => $did, 'seek_name' => $seekOne['title'],
                'pat_id' => $patid, 'payStatus' => 1,
            ];
        } else {
            $arys = [
                'zx_id' => $res['id'], 'did' => $res['id'], 'seek_name' => $seekOne['title'],
                'pat_id' => $res['patient_id'], 'payStatus' => $res['is_pay'],
            ];
            $zxid = $res['id'];
        }
        $arys['to_pay'] = [
            'yw_type' => 1,
            'yw_id' => $zxid,
        ];
        return $this->toback(0, '', $arys);
    }

    //设置咨询窗口过期
    public function guoqi()
    {
        //支付过期
        $addtime = date('Y-m-d H:i:s', time() - 3600);
        $this->dbConn($this->dbUdsPatient)
            ->where('is_pay', 1)
            ->where('addtime', '<', $addtime)
            ->update(['is_pay' => 3]);
        //咨询过期
        $this->dbConn($this->dbUdsPatient)
            ->where('is_pay', 2)
            ->where('paytime', '<', $addtime)
            ->update(['is_pay' => 4]);
        return true;
    }

    //初始化内容
    public function cntInit($zxid)
    {
        $zxOne = (array) $this->dbConn($this->dbUdsPatient)
            ->where('id', $zxid)
            ->first();
        if (empty($zxOne)) {
            return $this->toback(-2, '咨询错误');
        }
        $total = $this->dbConn($this->dbUdsp_cnt)
            ->where('zx_id', $zxid)
            ->count();
        if (!$total) {
            $cnt = '你好，为了更好了解到您的病情，请先填写一下调查表';
            $this->dbConn($this->dbUdsp_cnt)->insertGetId([
                'zx_id' => $zxid, 'doctor_id' => $zxOne['doctor_id'],
                'patient_id' => $zxOne['patient_id'], 'addtime' => toDate(),
                'cnt' => $cnt,
            ]);
        }
        $res = $this->dbConn($this->dbUdsp_cnt)
            ->where('zx_id', $zxid)
            ->orderBy('id', 'asc')
            ->get();
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $type = $v['type'];
            if ($type == 1) {
                $cnt = $v['cnt'];
            } else {
                $blk = (array) $this->dbConn($this->dbPatient)
                    ->where('del', 0)
                    ->where('id', $v['cnt'])
                    ->first();
                $cntTitle = !empty($blk) ? $blk['title'] : '-';
                $cnt = [
                    'id' => $v['cnt'],
                    'title' => $cntTitle,
                ];
            }
            $arys[] = [
                'id' => $v['id'],
                'zx_id' => $v['zx_id'],
                'doctor_id' => $v['doctor_id'],
                'patient_id' => $v['patient_id'],
                'type' => $v['type'],
                'cnt' => $cnt,
                'addtime' => $v['addtime'],
            ];
        }
        return $arys;
    }

    //获取内容
    public function getCntList($zxid)
    {
        $res = $this->cntInit($zxid);
        return $this->toback(0, '', $res);
    }

    //医生发送
    public function doctorAdd($zxid, $did, $cnt)
    {
        $cntOne = (array) $this->dbConn($this->dbUdsp_cnt)
            ->where('zx_id', $zxid)
            ->first();
        if (empty($cntOne)) {
            return $this->toback(-2, '内容错误');
        }
        $lastOne = [
            'zx_id' => $zxid, 'doctor_id' => $did, 'patient_id' => $cntOne['patient_id'],
            'cnt' => $cnt, 'addtime' => toDate(),
        ];
        $lastOne['id'] = $this->dbConn($this->dbUdsp_cnt)->insertGetId($lastOne);
        if (!$lastOne['id']) {
            return $this->toback(-1, 'error');
        }
        return $this->toback(0, '', $lastOne);
    }

    //病人发送
    public function patientAdd($zxid, $patid, $cnt)
    {
        $cntOne = (array) $this->dbConn($this->dbUdsp_cnt)
            ->where('zx_id', $zxid)
            ->first();
        if (empty($cntOne)) {
            return $this->toback(-2, '内容错误');
        }
        $lastOne = [
            'zx_id' => $zxid, 'doctor_id' => $cntOne['doctor_id'], 'patient_id' => $patid,
            'type' => 1, 'cnt' => $cnt, 'addtime' => toDate(),
        ];
        $lastOne['id'] = $this->dbConn($this->dbUdsp_cnt)->insertGetId($lastOne);
        if (!$lastOne['id']) {
            return $this->toback(-1, 'error');
        }
        return $this->toback(0, '', $lastOne);
    }

    /**
     * 患者病例列表
     * @param int $uid
     */
    public function patBingliList($uid)
    {
        $res = $this->dbConn($this->dbPatient)
            ->where('del', 0)
            ->where('uid', $uid)
            ->get();
        if (!count($res)) {
            return $this->toback();
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
            ];
        }
        return $this->toback(0, '', $arys);
    }

    /**
     * 患者已发送病例列表
     */
    public function bingliSendOkList($zxid)
    {
        $res = $this->dbConn($this->dbUdsp_cnt)
            ->where('zx_id', $zxid)
            ->where('type', 2)
            ->get();
        if (!count($res)) {
            return $this->toback();
        }
        $arys = [];
        foreach ($res as $v) {
            $blk = (array) $this->dbConn($this->dbPatient)
                ->where('id', $v->cnt)
                ->first();
            if (!empty($blk)) {
                $arys[] = [
                    'id' => $blk['id'],
                    'title' => $blk['title'],
                ];
            }
        }
        return $this->toback(0, '', $arys);
    }

    /**
     * 患者发送病例
     */
    public function bingliSend($zxid, $uid, $cnt)
    {
        $seekOne = (array) $this->dbConn($this->dbUdsPatient)
            ->where('id', $zxid)
            ->first();
        if (empty($seekOne)) {
            return $this->toback(-2, '没有窗口');
        }
        $cntOne = (array) $this->dbConn($this->dbUdsp_cnt)
            ->where('zx_id', $zxid)
            ->where('patient_id', $uid)
            ->where('type', 2)
            ->where('cnt', $cnt)
            ->first();
        if (!empty($cntOne)) {
            return $this->toback(-2, '该病例已发送');
        }
        $lastData = [
            'zx_id' => $zxid, 'doctor_id' => $seekOne['doctor_id'], 'patient_id' => $uid,
            'type' => 2, 'cnt' => $cnt, 'addtime' => toDate(),
        ];
        $lastOne['id'] = $this->dbConn($this->dbUdsp_cnt)->insertGetId($lastData);
        if (!$lastOne['id']) {
            return $this->toback(-1, 'error');
        }
        return $this->toback(0, '', $lastOne);
    }
}